﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyDoanVien.Bussiness;
using QuanLyDoanVien.Controller;

namespace QuanLyDoanVien
{
    public partial class frmQLHDHS : DevComponents.DotNetBar.Office2007Form
    {


        public frmQLHDHS()
        {
            InitializeComponent();
        }

        #region Biến
        string[] mucDo = { "Tốt", "Khá", "Trung bình" };
        List<HocSinhInfo> dsHS = new List<HocSinhInfo>();
        List<HoatDongInfo> dsHD = new List<HoatDongInfo>();
        AutoCompleteStringCollection autoHocSinh = new AutoCompleteStringCollection();
        AutoCompleteStringCollection autoHoatDong = new AutoCompleteStringCollection();
        HocSinhCtrl m_HocSinhCtrl = new HocSinhCtrl();
        HoatDongCtrl m_HoatDongCtrl = new HoatDongCtrl();
        HoatDongHocSinhCtrl m_HoatDongHSCtrl = new HoatDongHocSinhCtrl();
        List<LopInfo> ds_lop = new List<LopInfo>();
        LopCtrl m_LopCtrl = new LopCtrl();
        #endregion
        #region Hàm
        private void loadDSLop()
        {
            ds_lop = m_LopCtrl.Lay_DSLop();

            combTenLop.ValueMember = "MaLop";
            combTenLop.DisplayMember = "TenLop";
            combTenLop.DataSource = ds_lop;
        }
        private bool checkDataValid()
        {
            if (combTenHoatDong.Items.Count < 1)
            {
                MessageBox.Show("Vui lòng chọn hoạt động", "Thông báo");
                return false;
            }
            if (combTenLop.Items.Count < 1)
            {
                MessageBox.Show("Vui lòng chọn học sinh", "Thông báo");
                return false;
            }

            return true;
        }



        private void loadDSHoatDong()
        {
            dsHD = m_HoatDongCtrl.layDSHoatDong();
            foreach (HoatDongInfo hd in dsHD)
            {
                autoHoatDong.Add(hd.TenHoatDong);
            }
            combTenHoatDong.AutoCompleteCustomSource = autoHoatDong;
            combTenHoatDong.DisplayMember = "TenHoatDong";
            combTenHoatDong.ValueMember = "MaHoatDong";
            combTenHoatDong.DataSource = dsHD;

        }

        private void resetForm()
        {
            txt_HDHS_GhiChu.Text = "";
        }

        private void loadDSHoatDongHS()
        {
            if (combTenLop.SelectedIndex >= 0)
            {
                string maLop = combTenLop.SelectedValue.ToString();
                string maHoatDong = combTenHoatDong.SelectedValue.ToString();
                dsHS = m_HocSinhCtrl.layHocSinhTheoLop(maLop);
                dataGridView_HDHS.Rows.Clear();
                foreach (HocSinhInfo hs in dsHS)
                {
                    HoatDongInfo hd = (HoatDongInfo)combTenHoatDong.SelectedItem;
                    HoatDongHocSinhInfo hdhs = m_HoatDongHSCtrl.layHDHS(maHoatDong, hs.MaHocSinh);
                   
                    if((!chxHS_PhanCong.Checked&& (hdhs==null||hdhs.PhanCong==0) || (chxHS_PhanCong.Checked&&hdhs!=null&&hdhs.PhanCong==1)))
                    dataGridView_HDHS.Rows.Add(hs.MaHocSinh, hs.HoTen, hd.MaHoatDong, hd.TenHoatDong, chxHS_PhanCong.Checked, hd.GhiChu);

                }
            }

        }
        #endregion


        private void frmQLHDHS_Load(object sender, EventArgs e)
        {

            loadDSHoatDong();
            loadDSHoatDongHS();
            loadDSLop();
            resetForm();
        }

        private void combTenHS_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (combTenLop.SelectedIndex >= 0)
            {
                loadDSHoatDongHS();

            }
            else
            {

            }
        }

        private void btn_HDHS_Them_Click(object sender, EventArgs e)
        {
            resetForm();
        }

        private void dataGridView_HDHS_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btn_HDHS_Luu_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridViewRow dr in dataGridView_HDHS.Rows)
                {
                    if (!dr.IsNewRow)
                    {
                        HoatDongHocSinhInfo hdhs = new HoatDongHocSinhInfo();
                        hdhs.MaHocSinh = dr.Cells["MaHocSinh"].Value.ToString();
                        hdhs.MaHoatDong = dr.Cells["MaHoatDong"].Value.ToString();
                        hdhs.MucDo = "0";
                        hdhs.PhanCong = dr.Cells["PhanCong"].Value.ToString().ToLower() == "true" ? 1 : 0;
                        hdhs.GhiChu = dr.Cells["GhiChu"].Value.ToString();
                        hdhs.ThamGia = 0;
                        if (m_HoatDongHSCtrl.layHDHS(hdhs.MaHoatDong, hdhs.MaHocSinh) != null)
                        {
                            m_HoatDongHSCtrl.capNhatHoatDongHS(hdhs);
                        }
                        else
                            m_HoatDongHSCtrl.themHoatDongHS(hdhs);
                    }
                }
                MessageBox.Show("Cập nhật hoạt động học sinh thành công", "Thông báo");
                loadDSHoatDongHS();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cập nhật hoạt động học sinh thất bại" + ex.Message, "Thông báo");
            }

        }

        private void combTenHoatDong_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (combTenLop.SelectedIndex >= 0)
            {
                loadDSHoatDongHS();

            }
            else
            {

            }
        }

        private void chxHS_PhanCong_CheckedChanged(object sender, EventArgs e)
        {
            loadDSHoatDongHS();
        }


    }
}